import router from './router'
// import Cookies from 'js-cookies'

// import store from './store'
// import { Message } from 'element-ui'
// import // // NProgress from '// // NProgress' // progress bar
// import '// // NProgress/// // NProgress.css' // progress bar style
// import { getToken, setToken } from '@/utils/auth' // get token from cookies
// import getPageTitle from '@/utils/get-page-title'

// // // NProgress.configure({ showSpinner: false }) // // // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async (to, from, next) => {
    // start progress bar
    // // NProgress.start()

    // set page title
    // document.title = getPageTitle(to.meta.title)

    const token = to.fullPath.split('token=')[1]
    // console.log('toto',this.);
    if (token) {
        // setToken(token)
        localStorage.setItem('tokensty', token)
        // Cookies.set('tokensty', token)
    }
    // determine whether the user has logged in
    const hasToken = localStorage.getItem('tokensty')
    // const hasToken=
    if (hasToken) {
        if (to.path === '/login') {
            // if is logged in, redirect to the home page
            next({ path: '/' })
            // // NProgress.done()
        } else if (to.fullPath.indexOf('token=') >= 0) {
            console.log(to.path );
            
            next({ path: to.path })
            // // NProgress.done()
        } else {
            // function hasPermission(roles, targetTitle) {
            //     for (const role of roles) {
            //         // Check if the current item's title matches the target title
            //         if (role.resourceName === targetTitle) {
            //             return true;
            //         }
            //         // If there are children, recursively check them
            //         if (Array.isArray(role.children) && role.children.length > 0) {
            //             if (hasPermission(role.children, targetTitle)) {
            //                 return true;
            //             }
            //         }
            //     }
            //     return false
            // }
            // const hasRoles = !!store.getters.dynamicRoutes && store.getters.dynamicRoutes.length > 0
            // if (hasRoles) {
            //     if (to.meta.title) {
            //         if (hasPermission(store.getters.permissionRoutes, to.meta.title)) {
            //             next()
            //         } else {
            //             next('/404')
            //         }
            //     } else {
            //         next()
            //     }

            // } else {
            //     try {
            //         // get user info
            //         const result = await store.dispatch('user/getRoutes')
            //         const accessRoutes = await store.dispatch('permission/generateRoutes', result)
            //         await store.dispatch('user/dynamicRoutes', accessRoutes)
            //         next()
            //     } catch (error) {
            //         // remove token and go to login page to re-login
            //         await store.dispatch('user/resetToken')
            //         Message.error(error || 'Has Error')
            //         // next(`/login?redirect=${to.path}`)
            //         // // // NProgress.done()
            //         window.location.href = import.meta.env.VUE_APP_LOGOUT_URL + window.location.href
            //     }
            // }
        }
    } else {
        /* has no token*/

        if (whiteList.indexOf(to.path) !== -1) {
            // in the free login whitelist, go directly
            next()
        } else {
            
            // other pages that do not have permission to access are redirected to the login page.
            // next(`/login?redirect=${to.path}`)
            // // // NProgress.done()
            window.location.href = 'https://portal.hlbenkict.com/auth/logout?appCode=SHOP_APP&redirect=' + window.location.href
        }
    }
})

router.afterEach(() => {
    // finish progress bar
    // // // NProgress.done()
})
